package com.woniuxy.movie.commons.web.aop;

import cn.hutool.json.JSONUtil;
import com.woniuxy.movie.commons.web.annotation.MovieLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Slf4j
public class MovieLogAdvice {
    @Around("@annotation(com.woniuxy.movie.commons.web.annotation.MovieLog)")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {
        try {
            //输出日志
            MethodSignature ms = (MethodSignature) pjp.getSignature();
            Method method = ms.getMethod();
            MovieLog ml = method.getDeclaredAnnotation(MovieLog.class);
            log.info("=========开始执行{}===========",ml.value());
            log.info("参数：{}",JSONUtil.toJsonStr(pjp.getArgs()));
            Long start = System.currentTimeMillis();
            Object obj = pjp.proceed();
            //输出日志
            log.info("返回：{}", JSONUtil.toJsonStr(obj));
            log.info("耗时：{}",System.currentTimeMillis()-start);
            log.info("======================{}结束===============",ml.value());
            return obj;
        } catch (Throwable e) {
            throw e;
        }

    }
}
